<template>
  <el-dialog :title="titleId" :visible="showDialog" width="150" @close="cancel">
    <!-- 表单 -->
    <el-form label-width="120px"
    :model="amend"
    :rules="rules"
    ref="staff">
      <el-form-item
      label="学科名称"
      prop="subjectName"
      >
        <el-input style="width:50%" placeholder="请输入学科名称" v-model="amend.subjectName"/>
      </el-form-item>
      <el-form-item label="是否显示" prop="isFrontDisplay">
      <el-switch
      v-model="amend.isFrontDisplay"
      active-color="#13ce66"
      inactive-color="#ff4949"
      :active-value="1"
      :inactive-value="0">
      </el-switch>
    </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="cancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addsubject, amendsubject } from '@/api/hmmm/jjqsubject'
export default {
  data () {
    return {
      title: '',
      rules: {
        subjectName: [
          // required设置是否必填 如果不设置会根据校验规则自动设置
          { required: true, message: '课程名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    amend: {
      type: Object
    },
    titleId: {
      type: String
    }
  },
  methods: {
    async btnOk () {
      try {
        if (this.titleId === '新增学科') {
          await this.$refs.staff.validate()
          await addsubject(this.amend)
          this.$message.success('添加成功')
          this.$emit('update:showDialog', false)
          this.$parent.getsubjectsList()
          this.$parent.amend = {
            subjectName: '',
            isFrontDisplay: 1
          }
        } else {
          await amendsubject(this.amend)
          this.$message.success('修改成功')
          this.$emit('update:showDialog', false)
          this.$parent.getsubjectsList()
          this.$parent.amend = {
            subjectName: '',
            isFrontDisplay: 1
          }
        }
      } catch (error) {
        this.$message.error('添加失败')
      }
    },
    cancel () {
      this.$refs.staff.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
